<?php
namespace App\Services;
use App\Models\Balance;
use App\Models\User;

class BalanceService {

    public static function store($user_id, $point, $type, $is_transaction = true, $extra=null) {
        try {
            $is_transaction && \DB::beginTransaction();
            $search_key = 'user_id';
            $user = User::lockForUpdate()->find($user_id);
            if ($user) {
                $balanceModel = Balance::where($search_key, $user_id)->orderBy('id', 'desc')->first();
                if ($balanceModel) {
                    $balanceModel = Balance::where('id', $balanceModel->id)->first();
                }
                $balance = ($balanceModel->balance ?? 0) + $point ;
                if ($balance < 0)
                    throw new \Exception('粮票余额操作异常');

                $data = [
                    'user_id' => $user_id,
                    'type' => $type,
                    'point' => $point,
                    'date' => date('Ymd'),
                    'balance' => $balance,
                ];
                Balance::create($data);
                $user->balance = $balance;
                $user->save();
            }
            $is_transaction && \DB::commit();
        } catch (\Exception $e) {
            $is_transaction && \DB::rollBack();
            throw new \Exception($e->getMessage());
        }
    }

    public static function id_card($user_id) {
        if (!Balance::where('user_id', $user_id)->where('type', Balance::TYPE_ID_CARD)->exists())
            self::store($user_id, Balance::POINT_ID_CARD, Balance::TYPE_ID_CARD);
    }


    public static function article($user_id) {
        $cur_date = date('Ymd');
        if (!Balance::where('user_id', $user_id)->where('type', Balance::TYPE_ARTICLE)->where('date', $cur_date)->exists())
            self::store($user_id, Balance::POINT_ARTICLE, Balance::TYPE_ARTICLE);
    }

    public static function star($user_id) {
        $cur_date = date('Ymd');
        if (!$exist = Balance::where('user_id', $user_id)->where('type', Balance::TYPE_STAR)->where('date', $cur_date)->exists())
            self::store($user_id, Balance::POINT_STAR, Balance::TYPE_STAR);
        return $exist;
    }

    public static function comment($user_id) {
        $cur_date = date('Ymd');
        if (!$exist = Balance::where('user_id', $user_id)->where('type', Balance::TYPE_COMMENT)->where('date', $cur_date)->exists())
            self::store($user_id, Balance::POINT_COMMENT, Balance::TYPE_COMMENT);
        return $exist;
    }

    public static function focus($user_id) {
        $cur_date = date('Ymd');
        if (!Balance::where('user_id', $user_id)->where('type', Balance::TYPE_FOCUS)->where('date', $cur_date)->exists())
            self::store($user_id, Balance::POINT_FOCUS, Balance::TYPE_FOCUS);
    }

    public static function share($user_id) {
        $cur_date = date('Ymd');
        if (!Balance::where('user_id', $user_id)->where('type', Balance::TYPE_SHARE)->where('date', $cur_date)->exists())
            self::store($user_id, Balance::POINT_SHARE, Balance::TYPE_SHARE);
    }

    public static function watch_video($user_id) {
        $cur_date = date('Ymd');
        if (!Balance::where('user_id', $user_id)->where('type', Balance::TYPE_WATCH_VIDEO)->where('date', $cur_date)->exists())
            self::store($user_id, Balance::POINT_WATCH_VIDEO, Balance::TYPE_WATCH_VIDEO);
    }

    public static function sign_in($user_id, $cur_day, $last_week_count) {
        $cur_date = date('Ymd');
        if (!Balance::where('user_id', $user_id)->where('type', Balance::TYPE_SIGN_IN)->where('date', $cur_date)->exists()) {
            if ($last_week_count == 7) {
                $point = 15;
            } else {
                $point = 5;
                if ($cur_day <= 3) {
                    if ($cur_day == 2)
                        $point = 10;
                    else
                        $point = 5;
                } else if($cur_day == 4) {
                    $point = 10;
                } else if($cur_day == 5) {
                    $point = 5;
                } else if($cur_day == 6) {
                    $point = 10;
                } else if($cur_day >= 7) {
                    $point = 15;
                }
            }
            self::store($user_id, $point, Balance::TYPE_SIGN_IN);
        }
    }

    public static function is_finish($user_id) {
        $cur_date = date('Ymd');
        $arr['id_card'] = Balance::where('user_id', $user_id)->where('type', Balance::TYPE_ID_CARD)->exists();
        $arr['article'] = Balance::where('user_id', $user_id)->where('type', Balance::TYPE_ARTICLE)->where('date', $cur_date)->exists();
        $arr['star'] = Balance::where('user_id', $user_id)->where('type', Balance::TYPE_STAR)->where('date', $cur_date)->exists();
        $arr['comment'] = Balance::where('user_id', $user_id)->where('type', Balance::TYPE_COMMENT)->where('date', $cur_date)->exists();
        $arr['focus'] = Balance::where('user_id', $user_id)->where('type', Balance::TYPE_FOCUS)->where('date', $cur_date)->exists();
        $arr['share'] = Balance::where('user_id', $user_id)->where('type', Balance::TYPE_SHARE)->where('date', $cur_date)->exists();
        $arr['watch_video'] = Balance::where('user_id', $user_id)->where('type', Balance::TYPE_WATCH_VIDEO)->where('date', $cur_date)->exists();
        return $arr;
    }

}
